<!DOCTYPE html>
<html>

<?php

use Wgx\Admin\Com\AdminMenuRightsUtil;
use Wgx\Admin\Com\Crud\Crud;
use Wgx\Admin\Com\Crud\CrudField;
use Wgx\Admin\Com\Crud\EditPageInputBase;
use PhpYes\Apps;

/** @var Crud $crud */
$crud = empty($this->data['crud']) ? null : $this->data['crud'];
if (empty($crud)) {
    throw new \InvalidArgumentException('请传递crud');
}
$pageData = isset($this->data['data']) ? $this->data['data'] : null;

?>

<head>
  <?php require \PhpYes\Apps::render()->getPage('admin_page_parts/meta.html.php'); ?>
  <title>
    <?php
    $prefix = \PhpYes\Apps::getAdminPageTitlePrefix();
    $menuName = '';
    $currMenu = AdminMenuRightsUtil::getSingleObj()->getCurrentMenu();
    if ($currMenu) {
        $menuName = $currMenu->getName();
    }
    $opName = $crud->getOpName();
    $pageTitle = "{$prefix} - {$menuName} - {$opName}";
    echo $pageTitle;
    ?>
  </title>
  <meta name="description" content="index" />
  <link rel="stylesheet" href="/wgx_admin_static/fontawesome/5.3.1/css/all.min.css" />
  <?php
  $cssFileMap = $crud->getFileMap(Crud::PageEditPage, Crud::FileCssFile);
  if ($cssFileMap) {
      foreach ($cssFileMap as $file => $flag) {
          if ($flag) {
              echo '<link rel="stylesheet" href="' . $file . '" />' . "\n";
          }
      }
  }
  ?>
  <style>
    <?php
    $this->data['pageTitle'] = $pageTitle;
    require \PhpYes\Apps::render()->getPage('admin_page_parts/topbar_inline.css');
    ?>

    form.page-main-form {
      margin: 30px;
    }
    form.page-main-form .form_line {
      margin-top: 10px;
    }
    form.page-main-form .form_line>label {
      margin-right: 10px;
    }
    form.page-main-form .form_line .checkboxgroup {
      display: inline;
    }
    form.page-main-form .form_line .checkboxgroup>label {
      margin-left: 10px;
    }
    form.page-main-form .form_line .checkboxgroup>label:first-of-type {
      margin-left: 0px;
    }
    form.page-main-form .form_line .selectonline {
        display: inline;
    }
    form.page-main-form .form_line .inline-tips {
      display: inline-block;
      max-width: 20em;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-left: 10px;
      font-size: 0.8em;
      font-style: italic;
      color: gray;
      vertical-align: bottom;
    }
    form.page-main-form .form_line .popup-tips-indicator {
      margin-left: 10px;
      color: #65BCDA;
    }

    <?php
    $cssInlineFileMap = $crud->getFileMap(Crud::PageEditPage, Crud::FileCssInline);
    if ($cssInlineFileMap) {
        foreach ($cssInlineFileMap as $file => $flag) {
            if ($flag) {
                require $file ;
            }
        }
    }
    ?>
  </style>
</head>

<body>
  <?php require \PhpYes\Apps::render()->getPage('admin_page_parts/topbar_inline.html.php'); ?>
  <div id="main-container" class="main-container">
    <form id="page-main-form" class="page-main-form" method="post">
      <?php
      $isShowSubmit = false;
      $selectOnlineFieldArr = array();

      $fieldMap = $crud->getFieldMap();
      /**
       * @var string $fieldName
       * @var CrudField $field
       */
      foreach ($fieldMap as $fieldName => $field) {
          $showIn = intval($field->getShowIn());
          if (($showIn & CrudField::ShowInEditPage) < 1) {
              continue;
          }

          $inputHtml = '';
          $inputType = '';
          $origValue = null;
          $origValueStr = '';
          $isRequired = false;

          $inputWidget = $field->getEpInputWidget();
          if (empty($inputWidget)) {
              // $inputType = '';
              $origValue = isset($pageData) && isset($pageData[$fieldName]) ? $pageData[$fieldName] : '';
              $origValueStr = @strval($origValue);
              // $isRequired = false;
              $inputHtml = '<span>' . $origValue . '</span>';
          } else {
              $inputType = $inputWidget->getInputType();
              $origValue = $inputWidget->getOrigValue();
              $origValueStr = isset($origValue) ? @strval($origValue) : '';
              $isRequired = $inputWidget->getIsRequired();
              switch ($inputType) {
                  case EditPageInputBase::InputHidden:
                      $inputHtml = '<input type="hidden" name="' . $fieldName . '" value="' . $origValue . '" />';
                      break;
                  case EditPageInputBase::InputText:
                  case EditPageInputBase::InputNumber:
                  case EditPageInputBase::InputTel:
                  case EditPageInputBase::InputEmail:
                  case EditPageInputBase::InputPassword:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputInputBase $inputWidget */
                      $placeholder = $inputWidget->getPlaceholder();
                      $placeholder = empty($placeholder) ? null : trim($placeholder);
                      $className = $inputWidget->getClassName();
                      $className = empty($className) ? null : trim($className);
                      $specParam = $inputWidget->getSpecParam();
                      $specParam = empty($specParam) ? null : trim($specParam);
                      $inputHtml = '<input type="' . $inputWidget->getType() . '"'
                          . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($placeholder) ? '' : ' placeholder="' . $placeholder . '"')
                          . (empty($isRequired) ? '' : ' required')
                          . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputFile:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputColor:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputDateTime:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputDate:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputTime:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputMonth:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputWeek:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputCheckbox:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputCheckbox $inputWidget */
                      $className = $inputWidget->getClassName();
                      $className = empty($className) ? null : trim($className);
                      $specParam = $inputWidget->getSpecParam();
                      $specParam = empty($specParam) ? null : trim($specParam);
                      $isChecked = empty($origValue) ? 0 : intval($origValue);
                      $inputHtml = '<input type="checkbox"'
                          . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="1"'
                          . (empty($isChecked) ? '' : ' checked')
                          . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputCheckboxGroup:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputCheckboxGroup $inputWidget */
                      $map = $inputWidget->getMap();
                      $className = $inputWidget->getClassName();
                      $className = empty($className) ? null : trim($className);
                      $specParam = $inputWidget->getSpecParam();
                      $specParam = empty($specParam) ? null : trim($specParam);
                      $inputHtml = '<div class="checkboxgroup' . (empty($className) ? '' : " {$className}") . '"'
                          . (empty($specParam) ? '' : " {$specParam}")
                          . '>';
                      if ($map && is_array($map)) {
                          $origValueMap = array();
                          if ($origValue && is_array($origValue)) {
                              foreach ($origValue as $item) {
                                  $origValueMap[strval($item)] = 1;
                              }
                          }
                          foreach ($map as $k => $v) {
                              $inputHtml .= '<label><input type="checkbox" name="' . $k . '" value="' . $k . '"'
                                  . (empty($origValueMap[$k]) ? '' : ' checked')
                                  . ' /><span class="checkbox-label">' . $v . '</span></label>';
                          }
                      }
                      $inputHtml .= '</div>';
                      if ($origValue && is_array($origValue)) {
                          $origValueStr = implode(',', $origValue);
                      }
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputSelect:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputSelect $inputWidget */
                      $className = $inputWidget->getClassName();
                      $className = empty($className) ? null : trim($className);
                      $specParam = $inputWidget->getSpecParam();
                      $specParam = empty($specParam) ? null : trim($specParam);
                      $optionMap = $inputWidget->getOptionMap();
                      $inputHtml = '<select'
                          . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . (empty($specParam) ? '' : " {$specParam}")
                          . '>';
                      if (empty($optionMap)) {
                          $inputHtml .= '<option value="">空空空</option>';
                      } else {
                          foreach ($optionMap as $k => $v) {
                              $inputHtml .= '<option value="' . $k . '"' . ($k == $origValue ? ' selected' : '') . '>' . $v . '</option>';
                          }
                      }
                      $inputHtml .= '</select>';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputSelectOnline:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputSelectOnline $inputWidget */
                      $className = $inputWidget->getClassName();
                      $className = empty($className) ? null : trim($className);
                      $specParam = $inputWidget->getSpecParam();
                      $specParam = empty($specParam) ? null : trim($specParam);
                      $inputHtml = '<div class="selectonline' . (empty($className) ? '' : " {$className}") . '"'
                          . (empty($specParam) ? '' : " {$specParam}")
                          . '>';
                      $inputHtml .= '<select name="' . $fieldName . '">';
                      $inputHtml .= '<option></option>';
                      $inputHtml .= '</select>';
                      $ovHtml = $inputWidget->getOrigValueHtml();
                      $ovHtml = empty($ovHtml) ? null : trim($ovHtml);
                      if (empty($ovHtml)) {
                          $ovHtml = '无';
                      }
                      $inputHtml .= '(原值: ' . $ovHtml . ')</div>';
                      $isShowSubmit = true;
                      $selectOnlineFieldArr[] = array(
                          'fieldName' => $fieldName,
                          'hName' => $field->getHName(),
                          'url' => $inputWidget->getUrl(),
                          'searchFieldName' => $inputWidget->getSearchFieldName(),
                          'placeholder' => $inputWidget->getPlaceholder(),
                          'width' => $inputWidget->getWidth(),
                          'itemConverter' => $inputWidget->getItemConverter(),
                          'limit' => $inputWidget->getLimit(),
                          'orderFieldName' => $inputWidget->getOrderFieldName(),
                          'orderDirection' => $inputWidget->getOrderDirection(),
                      );
                      break;
                  case EditPageInputBase::InputTextarea:
                      $inputHtml = '<input type="text"'
                          // . (empty($className) ? '' : (' class="' . $className . '"'))
                          . ' name="' . $fieldName . '"'
                          . ' value="' . $origValue . '"'
                          . (empty($isRequired) ? '' : ' required')
                          // . (empty($specParam) ? '' : " {$specParam}")
                          . ' />';
                      $isShowSubmit = true;
                      break;
                  case EditPageInputBase::InputCustomize:
                      /** @var \Wgx\Admin\Com\Crud\EditPageInput\InputCustomize $inputWidget */
                      $specParam = $inputWidget->getSpecParam();
                      $inputHtml = empty($specParam) ? '' : trim($specParam);
                      $isShowSubmit = true;
                      break;
                  default:
                      $inputHtml = '<span>' . $origValue . '</span>';
                      break;
              }
          }

          $tips = $field->getTips();
          $tips = empty($tips) ? null : trim($tips);
          $tipsHtml = '';
          if ($tips) {
              $tipsHtml = '<span class="inline-tips"># ' . htmlspecialchars($tips, ENT_QUOTES | ENT_HTML5) . '</span>';
              if (mb_strlen($tips) >= 20) {
                  $tipsHtml .= '<i class="fas fa-info-circle popup-tips-indicator"></i>';
              }
          }
          echo '<div class="form_line field_' . trim($fieldName) . '"'
              . ' data-fieldname="' . $fieldName . '"'
              . ' data-hname="' . $field->getHName() . '"'
              . ' data-inputtype="' . $inputType . '"'
              . ' data-origval="' . (empty($origValueStr) ? $origValueStr : htmlspecialchars($origValueStr, ENT_QUOTES | ENT_HTML5)) . '"'
              . ' data-isrequired="' . (empty($isRequired) ? 0 : 1) . '"'
              . '>'
              . '<label>' . $field->getHName() . ':</label>'
              . '<span>' . $inputHtml . '</span>'
              . $tipsHtml
              . '</div>' . "\n";
      }
      if ($isShowSubmit) {
          echo '<div class="form_line"><button type="submit">提交</button></div>' . "\n";
      }
      ?>
    </form>
  </div>

  <script src="/wgx_admin_static/jquery-3.6.1.min.js"></script>
  <script src="/wgx_admin_static/layer/3.5.1/layer.js"></script>
  <?php
  if ($selectOnlineFieldArr && count($selectOnlineFieldArr) > 0) {
      echo '<link rel="stylesheet" href="/wgx_admin_static/select2/4.0.13/css/select2.min.css" />' . "\n";
      echo '<script src="/wgx_admin_static/select2/4.0.13/js/select2.full.min.js"></script>' . "\n";
      echo '<script src="/wgx_admin_static/select2/4.0.13/js/i18n/zh-CN.js"></script>' . "\n";
  }
  ?>
  <?php
  $jsFileMap = $crud->getFileMap(Crud::PageEditPage, Crud::FileJsFile);
  if ($jsFileMap) {
      foreach ($jsFileMap as $file => $flag) {
          if ($flag) {
              echo '<script src="' . $file . '"></script>' . "\n";
          }
      }
  }
  ?>
  <script>
    jQuery(function ($) {
      <?php
        if ($selectOnlineFieldArr && count($selectOnlineFieldArr) > 0) {
            echo <<<'REGISTERSELECTONLINE'

var registerSelectOnline = function (fieldName, url, searchFieldName, placeholder, width, itemConverter, limit, orderFieldName, orderDirection) {
  fieldName = fieldName && fieldName.trim() || null;
  if (!fieldName) { return; }
  url = url && url.trim() || null;
  if (!url) { return; }
  searchFieldName = searchFieldName && searchFieldName.trim() || null;
  if (!searchFieldName) {
    return;
  }
  var selector = 'form.page-main-form .form_line.field_' + fieldName + ' .selectonline select';

  placeholder = placeholder && placeholder.trim() || null;
  if (!placeholder) {
    placeholder = '请选择';
  }
  width = width && width.trim() || null;

  limit = limit && parseInt(limit) || 0;
  if (!limit) {
    limit = 10;
  }

  orderFieldName = orderFieldName && orderFieldName.trim() || '';
  orderDirection = orderDirection && parseInt(orderDirection) || 0;

  $(selector).select2({
    language: 'zh-CN',
    // theme: '',
    width: width,
    // placeholder: { // 本想这样配置来搞定默认选中，奈何 select2 的行为不是自己想要的，或许 select2 在这个地方有 bug
    //   id: defaultSelVal,
    //   text: defaultSelTxt
    // },
    placeholder: placeholder,
    allowClear: true,
    // dropdownCssClass: '',
    // dropdownAutoWidth: true,
    // templateResult: '',
    // escapeMarkup: '',
    // selectionCssClass: '',
    // templateSelection: '',
    // selectOnClose: false,
    // minimumInputLength: '',
    // maximumInputLength: '',
    ajax: {
      delay: 500,
      method: 'GET',
      url: url,
      // contentType: 'application/json; charset=UTF-8',
      data: function (params) {
        // console.log(params);
        var page = params.page && parseInt(params.page) || 0;
        if (page < 1) { page = 1; }
        var offset = limit * (page - 1);
        var myReq = { _limit: limit, _offset: offset, _orderFieldName: orderFieldName, _orderDirection: orderDirection };
        myReq[searchFieldName] = params.term && params.term.trim() || '';
        return myReq;
      },
      dataType: 'json',
      // beforeSend: function (jqXHR, settings) { },
      // complete: function (jqXHR, textStatus) { },
      processResults: function (data, params) { // 阅读 select2.js 的源代码，发现 processResults 用在 success 回调中
        // console.log(params);
        if (data && data.code && parseInt(data.code) < 400) {
          var total = data && data.data && data.data.total && parseInt(data.data.total) || 0;
          var rspData = data && data.data && data.data.data || null;
          if (!total || !rspData || !Array.isArray(rspData) || rspData.length < 1) {
            return {
              "results": [],
              "pagination": { "more": false }
            };
          }
          var results = [], i, item, more = false;
          if (rspData.length < limit) {
            more = false;
          } else {
            var page = params.page && parseInt(params.page) || 0;
            if (page < 1) { page = 1; }
            if (limit * page < total) {
              more = true;
            } else {
              more = false;
            }
          }
          for (i = 0; i < rspData.length; i++) {
            if (itemConverter) {
              item = itemConverter(i, rspData[i], rspData);
            } else {
              item = rspData[i];
            }
            results.push(item);
          }
          return {
            "results": results,
            "pagination": { "more": more }
          };
        } else {
          layer.alert(data && data.msg || '服务器错误，请稍后重试!', { title: '错误提示', icon: 0 });
          return {
            "results": [],
            "pagination": { "more": false }
          };
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        layer.alert('网络错误，请稍后重试!', { title: '错误提示', icon: 0 });
      }
    },
  });
};

REGISTERSELECTONLINE;
            foreach ($selectOnlineFieldArr as $field) {
                $fieldName = empty($field['fieldName']) ? null : trim($field['fieldName']);
                $url = empty($field['url']) ? null : trim($field['url']);
                $searchFieldName = empty($field['searchFieldName']) ? null : trim($field['searchFieldName']);
                if (empty($fieldName) || empty($url) || empty($searchFieldName)) {
                    continue;
                }
                $placeholder = empty($field['placeholder']) ? null : trim($field['placeholder']);
                if (empty($placeholder)) {
                    $hName = empty($field['hName']) ? '' : trim($field['hName']);
                    $placeholder = '请选择' . $hName;
                }
                $width = empty($field['width']) ? '' : trim($field['width']);
                $itemConverter = empty($field['itemConverter']) ? null : trim($field['itemConverter']);
                if (empty($itemConverter)) {
                    $itemConverter = 'null';
                }
                $limit = empty($field['limit']) ? 0 : intval($field['limit']);
                $orderFieldName = empty($field['orderFieldName']) ? '' : trim($field['orderFieldName']);
                $orderDirection = empty($field['orderDirection']) ? 0 : intval($field['orderDirection']);
                echo 'registerSelectOnline(' . "'{$fieldName}'"
                    . ',' . "'{$url}'"
                    . ',' . "'{$searchFieldName}'"
                    . ',' . "'{$placeholder}'"
                    . ',' . "'{$width}'"
                    . ',' . $itemConverter
                    . ',' . $limit
                    . ',' . "'{$orderFieldName}'"
                    . ',' . $orderDirection
                    . ');' . "\n";
            }
        }
      ?>
      var tipsLayerIdx = 0, tipsLayerField = null;
      $('#page-main-form').on('mouseenter', '.form_line .popup-tips-indicator', function (e) {
        e.preventDefault();
        e.stopPropagation();

        var tips = $(this).prev('.inline-tips').html();
        tips = tips && tips.trim() || null;
        if (!tips) {
          return;
        }
        if (tips[0] == '#') {
          tips = tips.substring(1).trim();
        }

        var formLine = $(this).closest('.form_line');
        var fieldName = formLine.data('fieldname');
        fieldName = fieldName && fieldName.trim() || null;
        if (!fieldName) {
          return;
        }

        if (tipsLayerField && tipsLayerField == fieldName) {
          return;
        }

        if (tipsLayerField) {
          layer.close(tipsLayerIdx);
          tipsLayerField = null;
          tipsLayerIdx = 0;
        }

        tipsLayerIdx = layer.tips(tips, this, {
          skin: 'field-popup-tips',
          area: '20em',
          closeBtn: 1,
          time: 0,
          tips: [2, 'rgba(0,0,0,0.5)'],
          success: function (layerDomObj, idx) {
            var jLayerDomObj = $(layerDomObj);
            var bgColor = jLayerDomObj.find('.layui-layer-content').css('background-color');
            jLayerDomObj.find('i.layui-layer-TipsG').css('border-bottom-color', 'transparent');
            jLayerDomObj.find('i.layui-layer-TipsR').css('border-right-color', bgColor).css('left', '-15px').css('top', '0px');
            jLayerDomObj.find('i.layui-layer-TipsL').css('border-left-color', bgColor).css('right', '-15px').css('top', '0px');
          },
          cancel: function (idx, layerDomObj) {
            tipsLayerField = null;
            tipsLayerIdx = 0;
          }
        });
        tipsLayerField = fieldName;
      });
      $('body').on('click', '.layui-layer-tips.field-popup-tips', function (e) {
        e.preventDefault();
        e.stopPropagation();
      });
      $('body').on('click', function (e) {
        if (tipsLayerField) {
          layer.close(tipsLayerIdx);
          tipsLayerField = null;
          tipsLayerIdx = 0;
        }
      });

      $('#page-main-form').submit(function (e) {
        e.preventDefault();
        e.stopPropagation();
        console.log(e);

        var postData = { id: 0 };
        var jForm = $(this);
        var formLines = jForm.find('div.form_line');
        var i, id, jLine, fieldName, origVal, inputType, inputVal, isRequired;
        for (i = 0; i < formLines.length; i++) {
          jLine = $(formLines[i]);
          fieldName = jLine.data('fieldname');
          fieldName = fieldName && fieldName.trim() || null;
          if (!fieldName) {
            continue;
          }
          origVal = jLine.data('origval');
          if (fieldName == 'id') {
            id = origVal && parseInt(origVal) || 0;
            postData['id'] = id;
            continue;
          }

          inputType = jLine.data('inputtype');
          inputType = inputType && inputType.trim() || null;
          if (!inputType) {
            continue;
          }
          isRequired = jLine.data('isrequired');
          isRequired = isRequired && parseInt(isRequired) || 0;

          inputVal = null;
          switch (inputType) {
            case '<?php echo EditPageInputBase::InputHidden; ?>':
            case '<?php echo EditPageInputBase::InputText; ?>':
            case '<?php echo EditPageInputBase::InputNumber; ?>':
            case '<?php echo EditPageInputBase::InputTel; ?>':
            case '<?php echo EditPageInputBase::InputEmail; ?>':
            case '<?php echo EditPageInputBase::InputPassword; ?>':
              inputVal = jLine.find('input[name="' + fieldName + '"]').val();
              if (isRequired) {
                if (inputVal === undefined || inputVal === null || inputVal === '') {
                  formLines.removeClass('highlight');
                  jLine.addClass('highlight');
                  layer.alert(jLine.data('hname') + '不能为空!', { title: '错误提示', icon: 0 });
                  return false;
                }
              }
              break;
            case '<?php echo EditPageInputBase::InputFile; ?>':
            case '<?php echo EditPageInputBase::InputColor; ?>':
            case '<?php echo EditPageInputBase::InputDateTime; ?>':
            case '<?php echo EditPageInputBase::InputDate; ?>':
            case '<?php echo EditPageInputBase::InputTime; ?>':
            case '<?php echo EditPageInputBase::InputMonth; ?>':
            case '<?php echo EditPageInputBase::InputWeek; ?>':
              layer.alert(inputType + '控件还没有实现，请使用已经实现了的控件!', { title: '错误提示', icon: 0 });
              return false;
              break;
            case '<?php echo EditPageInputBase::InputCheckbox; ?>':
              inputVal = jLine.find('input[name="' + fieldName + '"]').prop('checked');
              inputVal = inputVal ? 1 : 0;
              break;
            case '<?php echo EditPageInputBase::InputCheckboxGroup; ?>':
              inputVal = [];
              jLine.find('.checkboxgroup input[type="checkbox"]').each(function (idx, ele) {
                if ($(ele).prop('checked')) {
                  inputVal.push($(ele).val());
                }
              });
              if (isRequired && inputVal.length < 1) {
                formLines.removeClass('highlight');
                jLine.addClass('highlight');
                layer.alert(jLine.data('hname') + '不能为空!', { title: '错误提示', icon: 0 });
                return false;
              }
              break;
            case '<?php echo EditPageInputBase::InputRadioGroup; ?>':
              layer.alert(inputType + '控件还没有实现，请使用已经实现了的控件!', { title: '错误提示', icon: 0 });
              return false;
              break;
            case '<?php echo EditPageInputBase::InputSelect; ?>':
            case '<?php echo EditPageInputBase::InputSelectOnline; ?>':
              inputVal = jLine.find('select[name="' + fieldName + '"]').val();
              break;
            case '<?php echo EditPageInputBase::InputSelect2; ?>':
            case '<?php echo EditPageInputBase::InputSelect3; ?>':
            case '<?php echo EditPageInputBase::InputTextarea; ?>':
            case '<?php echo EditPageInputBase::InputCustomize; ?>':
              layer.alert(inputType + '控件还没有实现，请使用已经实现了的控件!', { title: '错误提示', icon: 0 });
              return false;
              break;
            default:
              layer.alert(inputType + '控件是无效的控件，请使用有效的控件!', { title: '错误提示', icon: 0 });
              return false;
              break;
          }
          postData[fieldName] = inputVal;
        }

        var loading = layer.load(2);
        $.ajax({
          method: 'POST',
          url: window.location.pathname,
          contentType: 'application/json',
          data: JSON.stringify(postData),
          dataType: 'json',
          success: function (data, textStatus, jqXHR) {
            // console.log(data);
            layer.close(loading);
            if (data && data.code) {
              var code = parseInt(data.code);
              if (code < 400) {
                layer.msg((id ? '修改' : '新增') + '成功!', { title: '提示', icon: 1, btn: '确定', closeBtn: 1, time: 0 }, function () {
                  if (window.parent && window.parent !== window) {
                    var selfIdx = window.parent.layer && window.parent.layer.getFrameIndex(window.name) || 0;
                    if (selfIdx) {
                      window.parent.layer.close(selfIdx);
                      window.parent.location.reload(true);
                    }
                  }
                });
              } else {
                if (code == 432) {
                  var field = data.field || null;
                  field = field && field.trim() || null;
                  var jForm = $('#page-main-form');
                  var formLines = jForm.find('div.form_line');
                  var i, jLine, fieldName;
                  for (i = 0; i < formLines.length; i++) {
                    jLine = $(formLines[i]);
                    fieldName = jLine.data('fieldname');
                    fieldName = fieldName && fieldName.trim() || null;
                    if (field && fieldName && fieldName == field) {
                      jLine.addClass('highlight');
                    } else {
                      jLine.removeClass('highlight');
                    }
                  }
                }
                layer.alert(data && data.msg || '服务器错误，请稍后重试!', { title: '错误提示', icon: 0 });
              }
            } else {
              layer.alert(data && data.msg || '服务器错误，请稍后重试!', { title: '错误提示', icon: 0 });
            }
          },
          error: function (jqXHR, textStatus, errorThrown) {
            layer.close(loading);
            layer.alert('网络错误，请稍后重试!', { title: '错误提示', icon: 0 });
          }
        });
      });
    });
    <?php
    $jsInlineFileMap = $crud->getFileMap(Crud::PageEditPage, Crud::FileJsInline);
    if ($jsInlineFileMap) {
        foreach ($jsInlineFileMap as $file => $flag) {
            if ($flag) {
                require $file;
            }
        }
    }
    ?>
  </script>
</body>

</html>